const app = getApp()

const { extend, Field, Stepper, Toast } = require('../../zanui/index')
import { formatTime, obj2Form } from '../../utils/util.js'
import { HOST } from '../../config/apiConfig.js'

Page(extend({}, Field, Stepper, Toast, {
  data: {
    id: '',
    scenicName: '',
    latitude: '',
    longitude: '',
    scenicAddress: '',
    businessStartTime: '09:00',
    businessEndTime: '18:00',
    ticketLastReserveTime: '10:00',
    productPriceExpiryStartDate: formatTime(new Date()).split(' ')[0],
    productPriceExpiryEndDate: formatTime(new Date()).split(' ')[0],
    reserveTime: 120,
    takeTicketStartTime: '09:00',
    takeTicketEndTime: '14:30',
    enterStartTime: '09:00',
    enterEndTime: '17:00',
    scenicIntroduce: '',
    ticketTypeName: '',
    productContain: '',
    normalPrice: '',
    ctripPrice: 0,
    ctripFloorPrice: 0,
    takeTicketWay: '',
    refundChangeRule: ''
  },
  onLoad: function (options) {
    const _self = this
    if (options.id != undefined) {
      wx.request({
        url: HOST + '/v1/scenic',
        data: {
          id: options.id
        },
        method: "GET",
        success: (res) => {
          const data = res.data.data[0]
          this.setData({
            id: data.id,
            scenicName: data.scenicName,
            latitude: data.latitude,
            longitude: data.longitude,
            scenicAddress: data.scenicAddress,
            businessStartTime: data.businessStartTime,
            businessEndTime: data.businessEndTime,
            ticketLastReserveTime: data.ticketLastReserveTime,
            productPriceExpiryStartDate: data.productPriceExpiryStartDate,
            productPriceExpiryEndDate: data.productPriceExpiryEndDate,
            reserveTime: data.reserveTime,
            takeTicketStartTime: data.takeTicketStartTime,
            takeTicketEndTime: data.takeTicketEndTime,
            enterStartTime: data.enterStartTime,
            enterEndTime: data.enterEndTime,
            scenicIntroduce: data.scenicIntroduce,
            ticketTypeName: data.ticketTypeName,
            productContain: data.productContain,
            normalPrice: data.normalPrice,
            ctripPrice: data.ctripPrice,
            ctripFloorPrice: data.ctripFloorPrice,
            takeTicketWay: data.takeTicketWay,
            refundChangeRule: data.refundChangeRule
          })
        }
      })
    }
    wx.setNavigationBarTitle({
      title: '景区基础信息'
    })
  },
  formSubmit(event) {
    const data = event.detail.value
    for (let key in data) {
      if (data[key] == '') {
        this.showZanToast('数据不能为空')
        return
      }
    }
    this.setData({
      submitDisabled: true
    })
    wx.showLoading({
      title: '加载中',
    })
    data.createBy = app.globalData.userInfo.openid
    data.areaId = app.globalData.areaInfo.id
    data.reserveTime = this.data.reserveTime
    const _self = this
    if (this.data.id == '' || this.data.id == undefined) {
      wx.request({
        url: HOST + '/v1/scenic',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: obj2Form(data),
        method: 'POST',
        success: function (res) {
          if (res.statusCode != 200) {
            _self.showZanToast('请求接口失败')
            return
          }
          if (res.data.code != '000000') {
            _self.showZanToast(res.data.message)
            return
          }
          wx.navigateTo({
            url: '../scenicDetail/scenicDetail?id=' + res.data.data.id,
          })
        },
        fail: function () {
          _self.showZanToast('网络错误')
        },
        complete: function () {
          _self.setData({
            submitDisabled: false
          })
          wx.hideLoading()
        }
      })
    } else {
      wx.request({
        url: HOST + '/v1/scenic/'+ _self.data.id,
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: obj2Form(data),
        method: 'PUT',
        success: function (res) {
          if (res.statusCode != 200) {
            _self.showZanToast('请求接口失败')
            return
          }
          if (res.data.code != '000000') {
            _self.showZanToast(res.data.message)
            return
          }
          wx.navigateTo({
            url: '../scenicDetail/scenicDetail?id=' + res.data.data.id,
          })
        },
        fail: function () {
          _self.showZanToast('网络错误')
        },
        complete: function () {
          _self.setData({
            submitDisabled: false
          })
          wx.hideLoading()
        }
      })
    }
  },
  handleZanStepperChange({ componentId, stepper }) {
    this.setData({
      [`${componentId}`]: stepper
    });
  },
  bindPickerChange(e) {
    this.setData({
      [`${e.currentTarget.id}`]: e.detail.value
    })
  },
  bindSelectMap() {
    const _self = this
    wx.chooseLocation({
      success: (data) => {
        _self.setData({
          latitude: data.latitude,
          longitude: data.longitude,
          scenicAddress: data.address
        })
      }
    })
  }
}))